<?php
class OrderModel extends Model {

	public function get_primary_key() {
		return $this->primary_key = 'id';
	}

	public function set($id, $data) {
		$id = intval($id);
	    if ($id) {
	        $this->update($data, 'id=' . $id);
	        return $id;
	    }
	    $this->insert($data);
	    if ($this->get_insert_id()) return $this->get_insert_id();
	    return false;
	}
	
	public function del($id) {
	    $this->delete('id=' . $id );
	}
	
	public function get_one_list($time){
		$sql = "select id from ".$this->prefix."order where addtime='".$time."'";
		$res = $this->db->query($sql)->row_array();
		return $res['id'];
	}
	
	public function get_one_data($id){
		$sql = "select * from ".$this->prefix."order where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res;
	}

    //根据sessid查询购物车产品数量
    public function get_cart_num($sessid){
        $sql = "select sum(number) as number from ".$this->prefix."cart where sessid='".$sessid."'";
        $res = $this->db->query($sql)->row_array();
        return $res['number'];
    }

	public function get_all_list($where,$order,$limit,$if_count=false){
	   if($if_count){
	   $sql = "select count(o.id) as total from ".$this->prefix."order o"."
			 left join ".$this->prefix."order_contact sc on o.id=sc.order_id"."
			 where $where";
			 $res = $this->db->query($sql)->row_array();
			 return $res['total'];
	   }
		 $sql = "select o.*,sc.id as scid,sc.name,sc.username,sc.address from ".$this->prefix."order o"."
			 left join ".$this->prefix."order_contact sc on o.id=sc.order_id"."
			 where $where order by $order limit $limit";
			//return $sql;
		$res = $this->db->query($sql)->result_array();
		foreach($res as $k=>$v){
			$res[$k]['shoparr'] = $this->getorder_shop($v['id']);
			$res[$k]['znum'] = $this->getorder_shop($v['id'],true);
		}
		
		return $res;
   }
   
   public function getorder_shop($id,$if_count=false){//获取每笔订单下的商品
   		if($if_count){
			$sql = "select sum(so.number) as total from ".$this->prefix."order_shop so"."
		left join ".$this->prefix."content_1 s on so.shop_id=s.id"."
		where so.order_id=$id";
			 $res = $this->db->query($sql)->row_array();
			 return $res['total'];
		}
   		$sql = "select so.*,s.thumb,s.title,s.pinpai from ".$this->prefix."order_shop so"."
		left join ".$this->prefix."content_1 s on so.shop_id=s.id"."
		where so.order_id=$id";
		return $this->db->query($sql)->result_array();
   }
   
   
   public function getorder_shopheji($id){
   		$sql = "select price from ".$this->prefix."order_shop where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res['price'];
   }
   
   public function get_show_list($id){//订单详情页获取内容
   		if($id){
			$sql = "select o.*,sc.id as scid,sc.name,sc.username,sc.address from ".$this->prefix."order o "."
			left join ".$this->prefix."order_contact sc on o.id=sc.order_id "."
			where o.id=$id";
			$res = $this->db->query($sql)->row_array();
			$res['shoparr'] = $this->getorder_shop($id);
			$res['znum'] = $this->getorder_shop($id,true);
			
			return $res;
		}
   }


    public function updateorder($id){
        $pay_time = time();
        $sql = "update ".$this->prefix."order set pay_status=2,pay_time=".$pay_time." where id=$id";
        $this->db->query($sql);
    }

    public function updateorder2($id){
        $pay_time = time();
        $sql = "update ".$this->prefix."order set pay_status=4,pay_time=".$pay_time." where id=$id";
        return $this->db->query($sql);
    }
	
	
	
	//后台 
	public function get_admin_list($where,$order,$limit,$if_count=false){
	   if($if_count){
	   $sql = "select count(id) as total from ".$this->prefix."order "."
			 where $where";
			 $res = $this->db->query($sql)->row_array();
			 return $res['total'];
	   }
		 $sql = "select * from ".$this->prefix."order "."
			 where $where order by $order limit $limit";
			//return $sql;
		$res = $this->db->query($sql)->result_array();
		foreach($res as $k=>$v){
			$res[$k]['shoparr'] = $this->getorder_shop($v['id']);
			$res[$k]['znum'] = $this->getorder_shop($v['id'],true);
			foreach($res[$k]['shoparr'] as $kk=>$vv){
				for($i=0;$i<$vv['number'];$i++){
					$res[$k]['zlirun'] += $this->ad_ordershop_lirun($vv['shop_id']);
				}
			}	
		}
		
		return $res;
   }
   
   
   public function ad_ordershop_lirun($id){
   		$sql = "select sum(profit) as total from ".$this->prefix."content_1 where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res['total'];
   }
   
   public function get_paystatus_list($where){
   	$sql = "select id from ".$this->prefix."order where pay_status=2 $where";
	return $this->db->query($sql)->result_array();
   }
   
   
   public function get_alljine($where){//全部已支付订单总金额
   	$sql = "select sum(price) as zonge,sum(yunfei) as zyunfei from ".$this->prefix."order where pay_status=2 $where";
	return $this->db->query($sql)->row_array();
   }
   
   public function get_zlirun($id){//全部已支付订单总利润
   		$shoparr = $this->getorder_shop($id);
		foreach($shoparr as $k=>$v){
			for($i=0;$i<$v['number'];$i++){
				$lirun += $this->get_onelirun($v['shop_id']);
			}
		}
		return $lirun;
   		
   }
   
   public function get_onelirun($id){
   	$sql = "select profit from ".$this->prefix."content_1 where id=$id";
	$res = $this->db->query($sql)->row_array();
	return $res['profit'];
   }
   
   public function get_adminorder_show($id){
   	$sql = "select o.*,sc.name,sc.username,sc.address,sc.info from ".$this->prefix."order o"."
	left join ".$this->prefix."order_contact sc on o.id=sc.order_id"."
	where o.id=$id";
	$res = $this->db->query($sql)->row_array();
	return $res;
   }


    public function get_wuliu_show($id){
        $sql = "select id,kuaidi,kuaidien,kuaidihao from ".$this->prefix."order where id=$id";
        return $this->db->query($sql)->row_array();
    }

    public function get_wuliu(){
        $sql = "select * from ".$this->prefix."order_wuliu";
        return $this->db->query($sql)->result_array();
    }


	//修改订单最终状态
	public function update_status($id){
		$sql = "update ".$this->prefix."order set status=4 where id=$id";
		$this->db->query($sql);
	}

	public function get_num($where){
        $sql = "select count(id) as total from ".$this->prefix."order where $where";
        $res = $this->db->query($sql)->row_array();
        return $res['total'];
    }
   
	   
	   
	   
	   
	   
	   
	   



}